# Inherit the parent CMake setting
set(CURRENT_SOURCE_DIR @CMAKE_CURRENT_SOURCE_DIR@)
set(CURRENT_BINARY_DIR @CMAKE_CURRENT_BINARY_DIR@)
set(CACHED_FILES_DIR @CACHED_FILES_DIR@)
set(LEMON_EXECUTABLE @LEMON_EXECUTABLE@)
set(RE2C_EXECUTABLE @RE2C_EXECUTABLE@)
set(PERPLEX_EXECUTABLE @PERPLEX_EXECUTABLE@)
set(LOCKED_SOURCE_DIR @LOCKED_SOURCE_DIR@)
set(DEBUGGING_GENERATED_SOURCES @DEBUGGING_GENERATED_SOURCES@)

if(NOT DEBUGGING_GENERATED_SOURCES)
  # Define a variety of convenience routines
  include(@PROJECT_CMAKE_DIR@/Generated_Source_Utils.cmake)

  # The following steps are executed to sync generated sources:
  #
  # 1.  Create a new verification_info.cmake file and populate
  #     it with the current versions of perplex, re2c and lemon.
  #     Also add the MD5 sums for current template files.
  #
  # 2.  For all files that need to be updated in the cache,
  #     calculate new MD5 sums and add them to the new
  #     verification_info.cmake - this is usually the input
  #     files and the generated outputs.
  #
  # 3.  Overwrite the original cached verification_info.cmake
  #     and generated files with the new ones.  If LOCKED_SOURCE_DIR
  #     is ON, this step will not be carried out - instead, an
  #     informational message with manual updating instructions
  #     will be printed.

  set(new_info_file "${CURRENT_BINARY_DIR}/verification_info.cmake.new")


  file(WRITE ${new_info_file} "# Autogenerated verification information\n")

  # Handle generator version numbers
  GET_GENERATOR_EXEC_VERSIONS()
  file(APPEND ${new_info_file} "set(baseline_lemon_version \"${lemon_version}\")\n")
  file(APPEND ${new_info_file} "set(baseline_re2c_version \"${re2c_version}\")\n")
  file(APPEND ${new_info_file} "set(baseline_perplex_version \"${perplex_version}\")\n")

  # Handle template files
  set(template_files "@TEMPLATE_FILELIST@")
  WRITE_MD5_SUMS("${template_files}" "${new_info_file}")

  # Handle input files
  set(input_files "@INPUT_FILELIST@")
  WRITE_MD5_SUMS("${input_files}" "${new_info_file}")

  # Handle generated files
  set(output_files "@BUILD_OUTPUT_FILELIST@")
  WRITE_MD5_SUMS("${output_files}" "${new_info_file}")

  # Copy files into their final locations
  if(NOT LOCKED_SOURCE_DIR)
    configure_file(${new_info_file} "${CACHED_FILES_DIR}/verification_info.cmake" COPYONLY)
    foreach(outf ${output_files})
      get_filename_component(filecorename ${outf} NAME)
      message("copying ${outf} to ${CACHED_FILES_DIR}/${filecorename}")
      configure_file(${outf} "${CACHED_FILES_DIR}/${filecorename}" COPYONLY)
    endforeach(outf ${output_files})
  else(NOT LOCKED_SOURCE_DIR)
    message("Source directory writing is locked - LOCKED_SOURCE_DIR is set.")
    message("To update the generated files, manually copy the following to ${CACHED_FILES_DIR}:")
    message("   ${new_info_file} (rename to verification_info.cmake)")
    foreach(outf ${output_files})
      message("   ${outf}")
    endforeach(outf ${output_files})
  endif(NOT LOCKED_SOURCE_DIR)

else(NOT DEBUGGING_GENERATED_SOURCES)

  message("\nNote: DEBUGGING_GENERATED_SOURCES is enabled - generated outputs will contain configuration-specific debugging information, so syncing cached output files is not possible.  To restore normal behavior, disable DEBUGGING_GENERATED_SOURCES.\n") 

endif(NOT DEBUGGING_GENERATED_SOURCES)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
